package interview.juc;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @Author: zhangrongqing
 * @Date: 2019/12/6 10:03
 */
public class ReentrantLockTestCase {
    public static void main(String[] args) {
        ReentrantLock lock = new ReentrantLock();
        for (int i = 0; i < 10; i++) {
            new Thread(new ThreadDemo2(lock)).start();
        }
    }
}

class ThreadDemo2 implements Runnable {

    private ReentrantLock lock;

    public ThreadDemo2(ReentrantLock lock) {
        this.lock = lock;
    }

    @Override
    public void run() {
        try {
            lock.lock();                         //同步锁，锁住
            Thread.sleep(1000);
            System.out.println(Thread.currentThread().getName() + "print");
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();                       //释放锁
        }

    }
}